package cn.bluedot.framework.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.bluedot.framework.dao.View;

/**
 * SQL解析器
 * @author Administrator
 *
 */
public class SqlParser {
	
	private static HashMap<String, View> mapper;
	
	static {
		try {
			mapper = XMLParser.getInstance().parseViews();
		} catch (Exception e) {
			System.out.println("service xml配置文件解析失败！");
		}
	}
	
	public HashMap<String, View> getMapper() {
		return mapper;
	}
	
	public static View getView(String name) throws Exception {
		if(mapper == null)
			mapper = XMLParser.getInstance().parseViews();
		return mapper.get(name);
	}
	
	public static StringBuffer mapToRexString(HashMap<String, String> map, String pattern) {
		
		StringBuffer buffer = new StringBuffer();
		
		Collection<String> values = map.values();
		
		List<String> list = new ArrayList<String>(values); 
		
		for(int i = 0; i < list.size(); i++) {
			
			if(i == list.size() - 1) {
				buffer.append(list.get(i));
			} else {
				buffer.append(list.get(i)).append(pattern);
			}
		}
		
		return buffer;				
	}
	
	private static StringBuffer mapToRexString(Map<String, Object> params, List<Object> param,
			HashMap<String, String> map, String pattern) {
		
		StringBuffer buffer = new StringBuffer();
		
		Collection<String> values = map.values();
		
		int count = 0;
		int size = values.size();
		
		for (String key : map.keySet()) {
			Object value = params.get(key);
			String column = map.get(key);
			
			if(count == (size - 1)) {
				buffer.append(column);
				count = 0;
			} else {
				buffer.append(column).append(pattern);
				count++;
			}
			param.add(value);
		}
		return buffer;
	}
		
	public static String insert(View view, Map<String, Object> params, List<Object> param) {
		
		StringBuffer buffer = new StringBuffer();
		String table = view.getTable();
		
		if(table == null) {
			buffer.append(view.getOther());
			return buffer.toString();
		}		
		
		buffer.append("insert into ").append(table).append(" (");
		
		HashMap<String, String> mapper = view.getMapper();
						
		StringBuffer mapperString = mapToRexString(params, param, mapper, ",");
		buffer.append(mapperString).append(") values (");
		
		for (int i = 0; i < mapper.size(); i++) {
			if(i == mapper.size()-1) {
				buffer.append("?)");
			} else {
				buffer.append("?,");
			}
		}
		
		return buffer.toString();
	}

	public static String delete(View view, Map<String, Object> params, List<Object> param) {
		
		StringBuffer buffer = new StringBuffer();
		String table = view.getTable();
		
		if(table == null) {
			buffer.append(view.getOther());
			return buffer.toString();
		}		
		
		buffer.append("delete from ").append(table).append(" where ");
		
		HashMap<String, String> where = view.getWhere();
						
		StringBuffer whereString = mapToRexString(params, param, where, "=? and ");
		buffer.append(whereString).append("=?");
		
		return buffer.toString();
	}
	
	public static String update(View view, Map<String, Object> params, List<Object> param) {
		
		StringBuffer buffer = new StringBuffer();
		String table = view.getTable();
		
		if(table == null) {
			buffer.append(view.getOther());
			return buffer.toString();
		}		
		
		buffer.append("update ").append(table).append(" set ");
		
		HashMap<String, String> mapper = view.getMapper();
		
		StringBuffer mapperString = mapToRexString(params, param, mapper, "=?,");
		buffer.append(mapperString).append("=? where ");
		
		
		HashMap<String, String> where = view.getWhere();
		
		StringBuffer whereString = mapToRexString(params, param, where, "=? and ");
		buffer.append(whereString).append("=?");
		
		return buffer.toString();
	}
	
	public static String select(View view, Map<String, Object> params, List<Object> param) {
		
		StringBuffer buffer = new StringBuffer();
		String table = view.getTable();
				
		if(table == null) {
			buffer.append(view.getOther());
			
			if(!params.isEmpty()) {
				for (String key : params.keySet()) { //可能存在顺序问题,推荐使用1，2，3代表参数值的键
					param.add(params.get(key));
				}
			}
			
			return buffer.toString();
		}		
		
		buffer.append("select ");
		
		HashMap<String, String> mapper = view.getMapper();
		
		if(mapper.isEmpty()) {
			buffer.append("* from ").append(table);
		} else {
			StringBuffer mapperString = mapToRexString(mapper, ",");
			buffer.append(mapperString).append(" from ").append(table);
		}
				
		String condition = view.getCondition();
		
		HashMap<String, String> where = view.getWhere();
		
		if(condition == null && where.isEmpty()) {
			return buffer.toString();
		}
	
		buffer.append(" where ");
				
		if(condition != null) {
			buffer.append(condition).append(" ");
			if(!where.isEmpty()) {
				buffer.append(" and ");
			}
		}
		
		if(!where.isEmpty()) {
			StringBuffer whereString = mapToRexString(params, param, where, "=? and ");
			buffer.append(whereString).append("=? ");
		}
				
		String other = view.getOther();
		
		//注意：如果是分页limit ?,? 需要在生成SQL后自行在param参数列表中添加分页的两个参数！！！！
		//如果字符串中存在 ? 需要在生成SQL后在param参数列表自行添加缺少的参数！！！！
		if(other != null) {  
			buffer.append(other);
		}
		
		return buffer.toString();
	}

}
